package service.sale.managers;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.util.ArrayList;

import article.entities.ArticleVO;
import article.exceptions.OutOfStockIngredientsException;
import sale.entities.FacturaVO;
import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptyOrderException;
import sale.exceptions.EmptySaleExcpetion;
import sale.exceptions.NoSalesException;
import sale.interfaces.SaleRemoteMgt;
import service.Factory;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

public class SaleProxyMgr implements SaleProxyMgt {

	/**
	 * Singleton implementation begin
	 */
	private static SaleProxyMgr instance = null;

	private SaleProxyMgr() {
	}

	public static SaleProxyMgr getInstance() {
		if (instance == null)
			instance = new SaleProxyMgr();
		return instance;
	}

	/**
	 * Singleton implementation end
	 * 
	 * @throws ProxyException
	 * @throws RemoteException
	 * @throws NoSalesException
	 */

	public SaleVO[] getOpenTableSales() throws ProxyException, RemoteException,
			NoSalesException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		return aux.getOpenSales();
	}

	public void newSale(OrderVO ord, int tableKey) throws ProxyException,
			RemoteException, EmptyOrderException,
			OutOfStockIngredientsException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		aux.newSale(ord, tableKey, ord.getCi());
	}

	public void addOrder(OrderVO ord, int tableKey) throws ProxyException,
			RemoteException, EmptySaleExcpetion, OutOfStockIngredientsException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		aux.addOrder(ord, tableKey);
	}

	public FacturaVO close(SaleVO sale) throws ProxyException, RemoteException,
			EmptySaleExcpetion {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		return aux.close(sale);
	}

	public void commit(FacturaVO fact) throws ProxyException, RemoteException {
		this.getSaleRemoteMgt().commit(fact);
	}

	public void deleteArticles(SaleVO sale, ArrayList<ArticleVO> arts)
			throws ProxyException, RemoteException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		aux.deleteArticles(sale, arts);
	}

	public boolean hasOpenSale(int tableKey) throws ProxyException,
			RemoteException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		return aux.hasOpenSale(tableKey);
	}

	public SaleVO getSale(int tableKey) throws ProxyException, RemoteException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		return aux.getSale(tableKey);
	}

	public int[] getOpenTables() throws ProxyException, RemoteException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		return aux.getOpenTables();
	}

	public SaleVO[] getOpenTakeoOutSales() throws ProxyException,
			RemoteException, NoSalesException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		return aux.getOpenTakeoOutSales();
	}

	public SaleVO[] getOpenDeliverySales() throws ProxyException,
			RemoteException, NoSalesException {
		SaleRemoteMgt aux = this.getSaleRemoteMgt();
		return aux.getOpenDeliverySales();
	}

	public void newDelSale(OrderVO ord, String address, String tel, int ci)
			throws ProxyException, RemoteException, EmptyOrderException,
			OutOfStockIngredientsException {
		if (ord != null)
			this.newSale(new OrderVO(ord, address, tel, ci), -1);
		else
			throw new EmptyOrderException("Empty Order");
	}

	public OrderVO[] getOpenOrders() throws ProxyException, RemoteException {
		return this.getSaleRemoteMgt().getOpenOrders();
	}

	public void newTakeOutSale(OrderVO ord) throws ProxyException,
			RemoteException, EmptyOrderException,
			OutOfStockIngredientsException {
		if (ord != null)
			this.newSale(ord, -2);
		else
			throw new EmptyOrderException("Empty Order");
	}

	private SaleRemoteMgt getSaleRemoteMgt() throws ProxyException {
		SaleRemoteMgt saleRem = null;
		try {
			String sSale = Factory.getInstance().getSale();
			Registry oRegitry = Factory.getInstance().getRegistry();
			saleRem = (SaleRemoteMgt) oRegitry.lookup(sSale);
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		if (saleRem == null)
			throw new ProxyException("Error en el RMI");
		return saleRem;
	}

	@Override
	public void setRedy(OrderVO ord) throws ProxyException, RemoteException {
		// TODO Auto-generated method stub
		
	}
}
